package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"softwareEngineering/models"
)

type LoginController struct {
	beego.Controller
}

func (c *LoginController) Login() {
	c.TplName = "login.html"
}

func (c *LoginController) HandleLogin() {
	username := c.GetString("username")
	userpw := c.GetString("userpw")
	o := orm.NewOrm()
	user := models.User{}
	user.Name = username
	user.Pwd = userpw
	err := o.Read(&user, "Name", "Pwd")
	if err != nil {
		c.TplName = "login.html"
	} else {
		c.Ctx.Output.Session("user", user)
		c.Data["user"] = user
		books := QueryBookData()
		c.Data["books"] = books
		c.Data["homeImg"] = books[0].HomePage
		c.TplName = "index.html"

	}
}
